package com.simple.quotes.gui;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author Petro
 */
public class ExitListener extends WindowAdapter implements ActionListener {
    private JFrame frame;

    ExitListener(JFrame frame) {
        this.frame = frame;
    }

    /**
     * Закрытие окна из самого окна (через пункт меню)
     * @param e event
     */
    public void actionPerformed(ActionEvent e) {
        exit();
    }

    /**
     * Закрытие окна по крестику или по Alt-F4
     * @param e event
     */
    public void windowClosing(WindowEvent e) {
        exit();
    }

    private void exit() {
        int selection = JOptionPane.showConfirmDialog(frame, "Exit program?", "Exit", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
        if (selection == JOptionPane.YES_OPTION) {
            frame.setVisible(false);
            System.exit(0);
        }
    }
}
